package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.AddroleDto;
import com.zzyl.dto.RolepageDto;
import com.zzyl.dto.updateDto;
import com.zzyl.entity.Role;
import com.zzyl.service.RoleService;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /*
     * 分页查询
     * */
    @PostMapping("//page/{pageNum}/{pageSize}")
    public ResponseResult<PageResponse<Role>> getRolePage(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize, @RequestBody RolepageDto dto) {
        PageResponse<Role> page = roleService.page(pageNum, pageSize, dto);
        return ResponseResult.success(page);
    }

    /*
     * 新增
     * */
    @PutMapping
    public ResponseResult addRole(@RequestBody AddroleDto dto) {
        roleService.addRole(dto);
        return ResponseResult.success();
    }

    /*
     * 4.3 根据角色查询选中的资源数据
     * */
    @GetMapping("/find-checked-resources/{roleId}")
    public ResponseResult<List<String>> findCheckedResources(@PathVariable("roleId") Long roleId) {
        List<String> list = roleService.findCheckedResources(roleId);
        return ResponseResult.success(list);

    }

    /*
     * 角色修改
     * */
    @PatchMapping
    public ResponseResult updateRole(@RequestBody updateDto dto) {
        roleService.updateRole(dto);
        return ResponseResult.success();
    }

    /*
    * 删除
    * */
    @DeleteMapping("/{id}")
    public ResponseResult deleteRole(@PathVariable("id") Long id) {
        roleService.deleteRole(id);
        return ResponseResult.success();
    }
    @ApiOperation("4.4 角色下拉框")
    @PostMapping("/init-roles")
    public ResponseResult<List<Role>> list() {
        List<Role> roles = roleService.list();
        return ResponseResult.success(roles);


    }

}

